
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using UMC.Data;


namespace UMC.WorkFlow
{
    /// <summary>
    /// 流程线
    /// </summary>
    public class Line : UMC.Data.IJSONType
    {
        /// <summary>
        /// 必须经过
        /// </summary>
        [DesignAttribute(Caption = "必须经过", ArgumentType = ArgumentType.NVSource, ArgumentValue = "是,True,否,False")]
        public bool IsMust
        {
            get; set;
        }
        [DesignAttribute(Caption = "描述")]
        public String Text { get; set; }
        /// <summary>
        /// 进入的工作点名字
        /// </summary> 
        public string ToId { get; set; }
        /// <summary>
        /// 出入的工作点名字
        /// </summary> 
        public string FromId { get; set; }
        /// <summary>
        /// 矢量的工作状态
        /// </summary>
        public LineStatus Status { get; set; }
        /// <summary>
        /// 在矢量唯一标志
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 当审批令牌经过时，所要做的事件
        /// </summary>
        /// <param name="shape">提交的</param>
        /// <param name="task"></param>
        public virtual void Activing(Shape shape, TaskInstance instance, TaskRuntime runtime)
        {

        }
        protected virtual string JSONName()
        {
            return this.GetType().Name;
        }
        public virtual Func<object> GetInstance(string prototypeName)
        {
            return () => String.Empty;
            //throw new NotImplementedException();
        }

        public virtual void Read(string key, object value)
        {
            switch (key)
            {
                case "IsMust":
                    this.IsMust = Convert.ToBoolean(value);
                    break;
                case "Id":
                    this.Id = value as string;
                    break;
                case "Status":
                    this.Status = Utility.Parse(value as string, LineStatus.UnPass);
                    break;
                case "FromId":
                    this.FromId = value as string;
                    break;
                case "ToId":
                    this.ToId = value as string;
                    break;
                case "Text":
                    this.Text = value as string;
                    break;
                    //,LineStatus.UnPass);
            }
        }
        protected virtual void WriteProperty(TextWriter writer)
        {

            writer.Write(',');
            JSON.Serialize("Id", this.Id, writer);
            if (String.IsNullOrEmpty(this.FromId) == false)
            {
                writer.Write(',');
                JSON.Serialize("FromId", this.FromId, writer);
            }
            if (String.IsNullOrEmpty(this.ToId) == false)
            {
                writer.Write(',');
                JSON.Serialize("ToId", this.ToId, writer);
            }
            if (String.IsNullOrEmpty(this.Text) == false)
            {
                writer.Write(',');
                JSON.Serialize("Text", this.Text, writer);
            }
            if (this.IsMust)
            {
                writer.Write(',');
                JSON.Serialize("IsMust", this.IsMust, writer);
            }
            if (this.Status > LineStatus.UnPass)
            {
                writer.Write(',');
                JSON.Serialize("Status", this.Status, writer);
            }
        }

        public void Write(TextWriter writer)
        {
            writer.Write('{');
            JSON.Serialize(JSON.Constructor, writer);
            writer.Write(":");
            JSON.Serialize(JSONName(), writer);
            WriteProperty(writer);
            writer.Write('}');
        }
    }
}
